package com.wfy.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.wfy.pojo.SpecGroup;
import com.wfy.pojo.SpecParam;
import com.wfy.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: SpecController
 * @Package com.wfy.controller
 * @Author 王飞遥
 * @Date 2022/8/22 19:45
 */
@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    SpecService specService;
    //传入规格关联id {cid}进行多表关联查询
    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid")Long cid){
        List<SpecGroup> groupList=specService.queryGroupByCid(cid);
        if(CollectionUtils.isEmpty(groupList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(groupList);
    }
    //添加组信息
    @PostMapping("/group")
    public ResponseEntity<List<SpecGroup>> saveGroup(@RequestBody SpecGroup specGroup){
        specService.saveGroup(specGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //查询param列表信息
    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "generic",required = false)Boolean generic,
            @RequestParam(value = "searching",required = false)Boolean searching
    ){
       List<SpecParam> specParamList= specService.queryParams(gid,cid,generic,searching);
       if(CollectionUtils.isEmpty(specParamList)){
           return ResponseEntity.notFound().build();
       }
       return ResponseEntity.ok(specParamList);
    }
    //添加
    @PostMapping("/param")
    public ResponseEntity<List<SpecParam>> saveParam(@RequestBody SpecParam specParam){
        specService.saveParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //修改
    @PutMapping("/param")
    public ResponseEntity<SpecParam> updateParam(@RequestBody SpecParam specParam){
        if(specParam==null){
            return ResponseEntity.badRequest().build();
        }
        specService.updateParam(specParam);
        return ResponseEntity.ok(specParam);
    }
    //删除
    @DeleteMapping("/param/{id}")
    public ResponseEntity delectParam(@PathVariable("id")Long id){
        int i=specService.delectParam(id);
        if(i<0){
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(i);
    }

}
